// 计算一致字符串
let alloweb = 'ab';
let words = ['ad','bd','aaab','baa','badab',];

function fnone(words:string[],alloweb:string){
    let obj = alloweb.split('')
    let count :number = 0;

        words.forEach((item,index)=>{
            let items = item.split('');
            let c= 0;
            items.forEach((v,i)=>{
                if(v === obj[0] || v===obj[1]){
                    c++;
                }
            })
            if(item.length === c){       
                count++;
            }
        })
    return count
}
console.log(fnone(words,alloweb));

// 获取url及封装深拷贝方法
function searUrl(url: string) {
    const paramStr = url.split('?')[1]
    const parArr = paramStr.split('&')
    // console.log(parArr);

    let obj = {}
    parArr.forEach((item: string) => {
        const key = item.split('=')[0]
        const value = item.split('=')[1]
        const newObj = {
            [key]: value
        }
        // console.log(newObj);
        // console.log(obj, 'obj');

        obj = {
            ...obj,
            ...newObj
        }
    })
    return obj
}
console.log(searUrl('http://example.com/index.html?a=1&b=2&c=3&d'));


// 买卖股票的最佳时机
function func(prices: number[]) {
    let profit = 0
    for (let i = 1; i < prices.length; i++) {
        let tmp = prices[i+1] - prices[i]
        if (tmp > 0) {
            profit += tmp
        }
    }
    return profit
}
console.log(func([7, 1, 5, 3, 6, 4]));


// 找只出现了一次的元素
function single(nums: number[]) {
    let num = -1
    for (let i = 0; i < nums.length; i++) {
        // 首次出现的位置                 最后一次出现的位置
        if (nums.indexOf(nums[i]) == nums.lastIndexOf(nums[i])) {
            num = nums[i]
        }
    }
    return num
}
console.log(single([1, 2, 1, 2, 4]));

// 多维转一维
// let narr: any[] = []
function flat<T>(arr: Array<T>, k:number): Array<T> {
    let narr: Array<T> = []
    arr.forEach((item: T) => {
        if (Array.isArray(item)) {   
            // flat(item, ++k)
            narr = narr.concat(flat(item, ++k))
        } else {
            // narr = narr.concat(item)   // [1, 2, 3, 4, 23, 4]
            narr.push(item)   //  [1, 2, 3, 4, 23, 4]
        }
    })
    return narr
}
console.log(flat([1, [2, 3, 4, [23, 4]]], 1));
